package com.tanhua.server.service;


import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.RecommendQueryVo;
import org.apache.dubbo.config.annotation.Reference;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mango.RecommendUser;
import com.tanhua.domain.vo.TodayBestVo;
import com.tanhua.dubbo.api.mongo.RecommendUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;

import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TodayBestService {

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private RecommendUserApi recommendUserApi;
    /**
     * 接口名称：今日佳人
     * @return
     */
    public ResponseEntity<Object> queryTodayBest() {
        //获取当前用户
        Long userId = UserHolder.getUserId();

        //根据当前登陆用户，调用Api接口，查询今日佳人
        //db.recommend_user.find({userId:20}).sort({score:-1}).limit(1)
        RecommendUser recommendUser = recommendUserApi.queryWithMaxScore(userId);
        if (recommendUser == null){
            recommendUser = new RecommendUser();
            recommendUser.setRecommendUserId(2L);
            recommendUser.setScore(80D);
        }

        //通过定义TodayBestVo对象，封装返回数据
        TodayBestVo todayBestVo = new TodayBestVo();
        //根据推荐的用户id查询用户信息（今日佳人）
        UserInfo userInfo = userInfoApi.findById(recommendUser.getRecommendUserId());
        if (userInfo != null){
            //封装信息
            BeanUtils.copyProperties(userInfo,todayBestVo);
            //设置tags
            if (userInfo.getTags() != null){
                todayBestVo.setTags(userInfo.getTags().split("."));
            }
        }
        //设置缘分值（调用Api查询的结果）
        todayBestVo.setFateValue(recommendUser.getScore().longValue());

        return ResponseEntity.ok(todayBestVo);
    }

    /**
     * 接口名称：推荐朋友 (首页推荐)
     */
    public ResponseEntity<Object> queryRecommendation(RecommendQueryVo vo) {
        //1. 获取当前登陆用户
        Long userId = UserHolder.getUserId();

        //2. 根据当前登陆用户，调用Api接口，分页查询推荐用户
        PageResult pageResult =
                recommendUserApi.queryRecommendation(userId,vo.getPage(),vo.getPagesize());
        //3. 获取查询到的数据
        List<RecommendUser> recommendUserList = (List<RecommendUser>) pageResult.getItems();
        if (recommendUserList == null) {
            // 如果查询为空，推荐用户默认就是id=5-10
            recommendUserList = new ArrayList<>();
            for (Integer i = 5; i < 10; i++) {
                RecommendUser ru = new RecommendUser();
                ru.setRecommendUserId(i.longValue());
                ru.setScore(70D+i);
                recommendUserList.add(ru);
            }
        }
        //4. 封装TodayBestVo集合对象
        //4.1 创建集合
        List<TodayBestVo> result = new ArrayList<>();
        //4.2 转换：把查询到的recommendUserList数据，封装到todayBestVo
        for (RecommendUser recommendUser : recommendUserList) {
            TodayBestVo todayBestVo = new TodayBestVo();
            UserInfo userInfo = userInfoApi.findById(recommendUser.getRecommendUserId());
            if (userInfo != null) {
                //封装用户信息
                BeanUtils.copyProperties(userInfo, todayBestVo);
                //设置tags
                if (userInfo.getTags() != null) {
                    todayBestVo.setTags(userInfo.getTags().split(","));
                }
                //设置缘分值（调用Api查询的结果）
                todayBestVo.setFateValue(recommendUser.getScore().longValue());

                //把封装好的vo对象，添加到集合
                result.add(todayBestVo);
            }
        }

        //4.3 把封装好的vo设置到pageResult中
        pageResult.setItems(result);

        //4. 把封装好的TodayBestVo设置到响应体中，返回给前端
        return ResponseEntity.ok(pageResult);
    }
}
